package org.glut.cloud.serviceRating.controller;

import lombok.extern.slf4j.Slf4j;
import org.glut.cloud.serviceRating.dto.MaintenanceRecords;
import org.glut.cloud.serviceRating.dto.ServiceEvaluationDTO;
import org.glut.cloud.serviceRating.entity.ServiceEvaluation;
import org.glut.cloud.serviceRating.feign.MaintenanceRecordsService;
import org.glut.cloud.serviceRating.service.IServiceEvaluationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 *  前端控制器
 *
 * @author qjc
 * @since 2024-08-13 16:33
 */
@Slf4j
@RestController
@RequestMapping("/service-evaluation")
public class ServiceEvaluationController {
    /**
     *qjc
     */
    @Autowired
    IServiceEvaluationService iserviceEvaluationService;

    @Autowired
    private IServiceEvaluationService iServiceEvaluationService;

    // 注入Feign客户端
    @Autowired
    private MaintenanceRecordsService maintenanceRecordsService;

    @GetMapping("/qq")
    public List<ServiceEvaluation> getAllEvaluations1() {
        return iserviceEvaluationService.list();
    }
    @GetMapping("/qq/{id}")
    public ServiceEvaluation getEvaluationById1(@PathVariable Integer id) {
        return iserviceEvaluationService.getById(id);
    }
    @PostMapping("/qq")
    public boolean addEvaluation1(@RequestBody ServiceEvaluation evaluation) {
        return iserviceEvaluationService.save(evaluation);
    }
    @PutMapping("/qq/{id}")
    public boolean updateEvaluation1(@PathVariable Integer id, @RequestBody ServiceEvaluation evaluation) {
        evaluation.setEvaId(id); // 确保 ID 设置到对象中
        return iserviceEvaluationService.updateById(evaluation);
    }
    @DeleteMapping("/{id}")
    public boolean deleteEvaluation1(@PathVariable Integer id) {
        return iserviceEvaluationService.removeById(id);
    }

        @GetMapping("/with-user")
    public List<ServiceEvaluationDTO> getServiceEvaluationsWithUserName() {
        return iserviceEvaluationService.getEvaluationsWithUserName();
    }


    /**wsy
     * 查询维修记录 远程
     * */
//    @Cacheable(value = "evaluation::selectRecords", key = "#userNo + '_' + #eva_status")
//    @LogAnnotation(module = "服务评价",operation = "查询维修记录")
    @PostMapping("/selectRecords/{userNo}/{evaStatus}")
    public List<MaintenanceRecords> selectRecords(@PathVariable Integer userNo,
                                                  @PathVariable String evaStatus){
        try {
            // 远程调用 repair-RepairRecordManage 模块的接口
//        return maintenanceRecordsService.select_1(userNo, evaStatus);
            return iServiceEvaluationService.selectRecords(userNo, evaStatus);
        } catch (Exception e) {
            // 处理异常，例如记录日志或返回错误信息
            log.error("调用 repair-RepairRecordManage 模块接口失败：", e);
            throw new RuntimeException("获取维修记录失败", e);
        }
    }

    /**wsy
     * 添加评价
     * */
    @PostMapping("/addEvaluation")
    public String addEvaluation(@RequestBody ServiceEvaluation serviceEvaluation) {
        try {
            // 【注意】 根据情况将task_id的值设置为null
            serviceEvaluation.setEvaTime(LocalDateTime.now());
            boolean result = iServiceEvaluationService.addEvaluation(serviceEvaluation);
            return result ? "评价成功" : "评价失败";
        } catch (Exception e) {
            // 记录错误日志
            log.error("添加评价时发生错误", e);
            return "服务器内部错误，请稍后再试";
        }
    }

    /**wsy
     * 查看评价
     * */
    @PostMapping("/selectEvaluation/{maintenanceRecordsId}")
    public ServiceEvaluation selectEvaluation(@PathVariable Integer maintenanceRecordsId) {
        try {
            return iServiceEvaluationService.selectById(maintenanceRecordsId);
        } catch (Exception e) {
            // 记录错误日志
            log.error("查看评价时发生错误", e);
            return null;
        }
    }

}
